//
//  SendRepairChooseVC.m
//  CJRailTransport
//
//  Created by Hank on 2018/1/30.
//  Copyright © 2018年 amplity. All rights reserved.
//

#import "SendRepairChooseVC.h"
#import "SendRepairChooseCell.h"
#import "UIButton+Expand.h"

@interface SendRepairChooseVC ()

@end

@implementation SendRepairChooseVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // 设置导航栏
    [self setNavC];
    // 设置界面属性
    [self setViewAttribute];
    // 注册Cell
    [self registerCells];
    // 测试数据
    [self testData];
    // 根据数据全选恢复全选状态
    [self recoverSelectAll];
}

#pragma mark - 导航栏
- (void)setNavC
{
    self.title = @"送修";
    // 设置导航栏颜色
    BasicNavigationController *nVC = (BasicNavigationController *)self.navigationController;
    nVC.themeColor = [UIColor whiteColor];
    nVC.tintColor = [UIColor darkGrayColor];
}

// 设置界面属性
- (void)setViewAttribute
{
    self.view.backgroundColor = [UIColor whiteColor];
}

// 注册Cell
- (void)registerCells
{
    UINib *nib = [UINib nibWithNibName:[SendRepairChooseCell reuseIdentifier] bundle:nil];
    [_myTableView registerNib:nib forCellReuseIdentifier:[SendRepairChooseCell reuseIdentifier]];
}

#pragma mark - initArray
// 初始化数组
- (NSMutableArray *)containerDataArray
{
    if (!_containerDataArray) {
        _containerDataArray = [[NSMutableArray alloc]init];
    }
    return _containerDataArray;
}

- (NSMutableArray *)allContainerDataArray
{
    if (!_allContainerDataArray) {
        _allContainerDataArray = [[NSMutableArray alloc]init];
    }
    return _allContainerDataArray;
}

// 测试数据
- (void)testData
{
    for (int i = 0; i < 5; i++) {
        // 临时判断类别
        [self.allContainerDataArray addObject:[NSString stringWithFormat:@"SCZU7854343%d",[self.type isEqualToString:@"1"] ? i : i + 5]];
    }
}
// 根据数据全选恢复全选状态
- (void)recoverSelectAll
{
    // 默认全选状态
    BOOL selectAllState = YES;
    // 遍历全部数据
    for (int i = 0; i < self.allContainerDataArray.count; i++) {
        if (![self.containerDataArray containsObject:self.allContainerDataArray[i]]) {
            selectAllState = NO;
            break;
        }
    }
    // 全选状态
    if (selectAllState) {
        // 增加全选标示
        _selectAllMark = @"selectAllMark";
        // 选中状态
        _checkImage.image = [UIImage imageNamed:@"select_button"];
    }else{
        // 移除全选标示
        _selectAllMark = @"";
        // 未选中状态
        _checkImage.image = [UIImage imageNamed:@"noSelect_button"];
    }
}

#pragma mark - Action
// 全选 Action
- (IBAction)selectAllAction:(UIButton *)sender
{
    if (JudgeStrEqualNull(_selectAllMark)) {
        // 增加全选标示
        _selectAllMark = @"selectAllMark";
        // 选中状态
        _checkImage.image = [UIImage imageNamed:@"select_button"];
        // 移除全部选中项
        [self.containerDataArray removeAllObjects];
        // 添加全部选中项
        for (int i = 0; i < self.allContainerDataArray.count; i++) {
            // 获取全部的Cell
            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
            SendRepairChooseCell *sendRepairChooseCell = [self.myTableView cellForRowAtIndexPath:indexPath];
            [self.containerDataArray addObject:sendRepairChooseCell.containerNo.text];
        }
        // 回调选择全部
        _allCheckImageClickBlock(self.containerDataArray, YES);
    }else{
        // 移除全选标示
        _selectAllMark = @"";
        // 未选中状态
        _checkImage.image = [UIImage imageNamed:@"noSelect_button"];
        // 回调取消选择全部
        _allCheckImageClickBlock(self.allContainerDataArray, NO);
        // 移除全部选中项
        [self.containerDataArray removeAllObjects];
    }
    // 刷新表
    [self.myTableView reloadData];
}

#pragma mark - TableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.allContainerDataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 防止循环引用
    WEAK_REF(self);
    
    SendRepairChooseCell *cell = [tableView dequeueReusableCellWithIdentifier:[SendRepairChooseCell reuseIdentifier] forIndexPath:indexPath];
    cell.indexPath = indexPath;
    [cell setData:self.allContainerDataArray selectData:self.containerDataArray];
    // 复选框点击 Block
    [cell setCheckImageClickBlock:^(NSIndexPath *indexPath, NSString *content, BOOL state){
        //======修改Model 自定义字段======
        if (state) {
            [weak_self.containerDataArray addObject:content];
            // 根据数据全选恢复全选状态
            [self recoverSelectAll];
        }else{
            // 移除指定元素
            [weak_self.containerDataArray removeObject:content];
            // 根据数据全选恢复全选状态
            [self recoverSelectAll];
        }
        // 刷新单个Cell
        [weak_self.myTableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationNone];
        // 回调数据
        weak_self.checkImageClickBlock(content, state);
    }];
    return cell;
}

#pragma mark - TableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
